package LC;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName LC113
 * @Description: TODO
 * @Author hl
 * @Date 2020/6/2
 * @Version V1.0
 **/
public class LC113 {

    public List<List<Integer>> pathSum(TreeNode root, int sum) {
     List<List<Integer>> result=new ArrayList<>();

     ArrayList<Integer> list=new ArrayList<>();

     dfs(root,sum,0,result,list);

     return  result;

    }

    private void dfs(TreeNode root, int sum, int i, List<List<Integer>> result, ArrayList<Integer> list) {

        if(root==null){
            return;
        }
        i=i+root.val;
        list.add(root.val);
        if(root.left==null&&root.right==null){
            if(sum==i){
                result.add(new ArrayList<>(list));

            }
            list.remove(list.size()-1);
            return;

        }
        dfs(root.left,sum,i,result,list);
        dfs(root.right,sum,i,result,list);
        list.remove(list.size()-1);

    }


}
